package com.example.demo.controller;

import com.alibaba.fastjson.JSON;
import com.example.demo.bean.BaseResponse;
import com.example.demo.bean.request.UserVerifyBean;
import com.example.demo.bean.response.AppInfoBean;
import com.example.demo.service.AppUseSwitchService;
import com.example.demo.util.ServletUtil;
import com.example.demo.util.json.JsonUtils;
import com.example.demo.util.rsa.RSAUtils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

import sun.rmi.runtime.Log;

@Controller
@RequestMapping("/appUse")
public class AppUseSwitchController {
    private static final Logger logger = LoggerFactory.getLogger(AppUseSwitchController.class);

    @Autowired
    private AppUseSwitchService appUseSwitchService;

    @RequestMapping(value = "/getAppInfo", method = RequestMethod.POST)
    @ResponseBody
    public String getAppInfo(HttpServletRequest req) {
        String _ReqJsonEncry = ServletUtil.getPostParams(req);
        logger.warn("请求密文=" + _ReqJsonEncry);
        BaseResponse _BaseResponse = null;
        try {
            String _ReqJson = RSAUtils.decryptPublicDef(_ReqJsonEncry);
            UserVerifyBean _UserVerifyBean = JsonUtils.fromJson(_ReqJson, UserVerifyBean.class);
            logger.warn("请求明文=" + _ReqJson);
            AppInfoBean _AppInfoBean = null;
            if (_UserVerifyBean != null) {
                _AppInfoBean = appUseSwitchService.getAppInfo(_UserVerifyBean.getPackageName());
                if (_AppInfoBean != null) {
                    _AppInfoBean.setTime(System.currentTimeMillis());
                }
            }
            _BaseResponse = new BaseResponse(_AppInfoBean);
        } catch (Exception e) {
            e.printStackTrace();
            _BaseResponse = new BaseResponse();
        }
        String _RespJsonEncrypt = JsonUtils.toJsonDefault(_BaseResponse);
        logger.info("响应明文=" + _RespJsonEncrypt);
        String _RespJson = RSAUtils.encryptPublicDef(_RespJsonEncrypt);
        logger.info("响应密文=" + _RespJson);
        return _RespJson;
    }
}
